package cn.seapon.talker;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RegisterPersonalActivity extends Activity implements
		OnClickListener {

	private EditText et_nickname;
	private EditText et_signature;
	private EditText et_phone;
	private EditText et_email;
	private Button btn_next;
	private RelativeLayout wait_net_frame = null;

	private Bundle prev;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_personal);
		et_nickname = (EditText) findViewById(R.id.nickname);
		et_signature = (EditText) findViewById(R.id.signature);
		et_phone = (EditText) findViewById(R.id.phone);
		et_email = (EditText) findViewById(R.id.email);
		btn_next = (Button) findViewById(R.id.next);
		wait_net_frame = (RelativeLayout) findViewById(R.id.wait_net_layout);

		prev = getIntent().getExtras();

		btn_next.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next:
			String phone = et_phone.getText().toString().trim();
			String email = et_email.getText().toString().trim();
			if (!TextUtils.isEmpty(phone)) {
				//简单的手机号码
				Pattern pattern = Pattern.compile("^1[0-9]{10}$");
				Matcher matcher = pattern.matcher(phone);
				if (!matcher.matches()) {
					Toast.makeText(this, "手机号码格式不对，正确格式为:\n1234567891", Toast.LENGTH_SHORT).show();
					return ;
				}
				prev.putString("phone", phone);
			}
			if (!TextUtils.isEmpty(email)) {
				Pattern pattern = Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
				Matcher matcher = pattern.matcher(email);
				if (!matcher.matches()) {
					Toast.makeText(this, "邮箱不合法！", Toast.LENGTH_SHORT).show();
					return ;
				}
				prev.putString("email", email);
			}

			String signature = et_signature.getText().toString().trim();
			if (!TextUtils.isEmpty(signature)) {

				prev.putString("signature", signature);
			}
			String nickname = et_nickname.getText().toString().trim();
			prev.putString("nickname", nickname);
			Intent intent = new Intent(RegisterPersonalActivity.this,
					RegisterFinishActivity.class);
			intent.putExtras(prev);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

}
